package com.springboot.demo.web;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.entity.Admin;
import com.springboot.demo.entity.Reader;
import com.springboot.demo.entity.User;
import com.springboot.demo.service.LibrarianService;
import com.springboot.demo.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")

public class LibrarianController {


    @Autowired
    private LibrarianService librarianService;
    @Autowired
    private ReaderService readerService;

    @RequestMapping(value = "/login")
    public String login(User user, Map<String,Object> map, HttpServletRequest httpServletRequest){
        int Success= librarianService.login(user).isSuccess();

        if(Success==1){
            httpServletRequest.getSession().setAttribute("phonenumber", user.getPhonenumber());
            return "librarian_homepage";
        }
        else if (Success==2) {
            Admin admin = new Admin();
            admin.setPhone(user.getPhonenumber());
            admin.setPassword(user.getPassword());
            admin.setLname(user.getLname());
            httpServletRequest.getSession().setAttribute("phonenumber", admin);
            return "admin_index";
        }

        else if(Success==3){
            map.put("msg", "用户名或密码错误，请重新登录");
            return "Librarian_login";
        }
        else{
            return "Librarian_login";
        }


    }
    @RequestMapping(value="/home")
    public String gethome(){
        return "reader_searchbook(no)";
    }
    @RequestMapping(value="/log")
    public String rr(){
        return "Librarian_login";
    }

}